﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Reflection;

namespace Pixysoft
{
    /// <summary>
    /// 预期之中的异常
    /// </summary>
    [Serializable]
    public abstract class ExpectedException : Exception
    {
        List<ExceptionHandleStructure> handlers = new List<ExceptionHandleStructure>();

        public ExpectedException()
        {
        }

        public ExpectedException(string message)
            : base(message)
        {
        }

        public ExpectedException(string message, Exception innerException)
            : base(message, innerException)
        {
        }

        /// <summary>
        /// 表示当前exception已经被处理了
        /// </summary>
        public ExpectedException Handle()
        {
            System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace(true);

            StackFrame frame = stack.GetFrame(1);

            MethodBase method = frame.GetMethod();

            ExceptionHandleStructure structure = new ExceptionHandleStructure();

            if (method != null)
            {
                structure.AssemblyName = Pixysoft.Tools.AssemblyHelper.Instance.GetAssemblyName(method.DeclaringType.Assembly);
                structure.ClassName = string.IsNullOrEmpty(method.DeclaringType.FullName) ? method.DeclaringType.Name : method.DeclaringType.FullName;
                structure.MethodName = method.Name;
            }

            handlers.Add(structure);

            return this;
        }

        /// <summary>
        /// 当前exception是否被处理过
        /// </summary>
        public bool IsHandled
        {
            get
            {
                return handlers.Count > 0;
            }
        }

        public ExceptionHandleStructure[] GetHandlers()
        {
            return handlers.ToArray();
        }
    }

    /// <summary>
    /// 预期之外的异常 永远不会开放给用户直接调用
    /// </summary>
    [Serializable]
    public class UnexpectedException : ExpectedException
    {
        public UnexpectedException()
        {
        }

        public UnexpectedException(string message)
            : base(message)
        {
        }

        public UnexpectedException(string message, Exception innerException)
            : base(message, innerException)
        {
        }
    }

    /// <summary>
    /// 输入错误的异常
    /// </summary>
    [Serializable]
    public class VerificationFailedException : ExpectedException
    {
        public VerificationFailedException()
        {
        }

        public VerificationFailedException(string message)
            : base(message)
        {
        }

        public VerificationFailedException(string message, Exception innerException)
            : base(message, innerException)
        {
        }
    }

    /// <summary>
    /// 外部的异常
    /// </summary>
    [Serializable]
    public class ExternalException : ExpectedException
    {
        public ExternalException()
        {
        }

        public ExternalException(string message)
            : base(message)
        {
        }

        public ExternalException(string message, Exception innerException)
            : base(message, innerException)
        {
        }
    }

    /// <summary>
    /// 返回未知结果的异常
    /// </summary>
    [Serializable]
    public class UnexpectedResultException : ExpectedException
    {
        public UnexpectedResultException()
        {
        }

        public UnexpectedResultException(string message)
            : base(message)
        {
        }

        public UnexpectedResultException(string message, Exception innerException)
            : base(message, innerException)
        {
        }
    }

    [Serializable]
    public class ExceptionHandleStructure
    {
        string assemblyName;
        string className;
        string methodName;

        public ExceptionHandleStructure()
        {
        }

        public ExceptionHandleStructure(string assembly, string classname, string method)
        {
            this.assemblyName = assembly;
            this.className = classname;
            this.methodName = method;
        }

        public string AssemblyName
        {
            get { return assemblyName; }
            set { assemblyName = value; }
        }
        public string ClassName
        {
            get { return className; }
            set { className = value; }
        }
        public string MethodName
        {
            get { return methodName; }
            set { methodName = value; }
        }

        public override string ToString()
        {
            return string.Format("{0}::{1}.{2}", assemblyName, className, methodName);
        }

    }
}
